<!DOCTYPE html>
<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8"
    contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>资源管理</title>
<jsp:include page="/common/head.jsp"></jsp:include>
<link href="<%=request.getContextPath()%>/static/plugin/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet">
<script src="<%=request.getContextPath()%>/static/plugin/zTree/js/jquery.ztree.all-3.5.min.js"></script>
<style>
    #content {
        position: absolute;
        top: 0;
        left: 240px;
        right: 10px;
        bottom: 0;
        box-sizing: border-box;
    }
    ul.ztree {
        width: 240px;
    }
</style>
</head>
<body class="iframe-page">
   <div class="tree-wrap">
    <div class="tree-title">资源列表</div>
    <div class="tree-content">
       <ul id="resourceList" class="ztree"></ul> 
    </div>
   </div>
   
    
     <div id="content">
        <iframe id="resourceFrame" frameborder="0" style="width:100%;height:100%" src="<%=request.getContextPath()%>/sys/resource/initStat"></iframe>
     </div>
     
     <div id="resourceMenu" style="width:140px;">
	      <div id="add">新建子节点</div>
	      <div id="edit">编辑节点</div>
	      <div id="delete">删除节点</div>
    </div>
     <script>
     var RESOURCE_URL = {
    		 INIT_MAIN: basePath + '/sys/resource/init',
    		 INIT_ADD: basePath + '/sys/resource/initAdd',
    		 INIT_EDIT: basePath + '/sys/resource/initEdit',
    		 INIT_VIEW: basePath + '/sys/resource/initView',
    		 INIT_STAT: basePath + '/sys/resource/initStat',
    		 DELETE: basePath + '/sys/resource/delete',
    		 GET_ALL: basePath + '/sys/resource/list?random=' + Math.random() * 1000000,
    		 GET_TREE: basePath + '/sys/inner/resourceTree',
     };
     var resourceFrame = document.getElementById('resourceFrame'),
         currentNode = null;
     
     // ztree 初始化设置 
     var setting = {  
         data: {
                simpleData: {
                    enable: true,
                    idKey: 'id',  // idkey
                    pIdKey: 'pId' // 父IDkey
                },
                key: {
                    url: 'zUrl' // url 字段名,解决字段名冲突问题
                }
            },
          callback: {
              onClick: viewResource,
              onRightClick: showResourceMenu
          },
         async: {
        	 enable: true,
        	 url: RESOURCE_URL.GET_TREE
         }
     },
     ztree;
     
     $(function() {
    	 // 初始化功能树
    	 initTree();
         
         $('#resourceMenu').menu({
             onClick: function(item){
             	if (item.id == 'add') {
             		addResource(currentNode);
             	} else if (item.id == 'edit') {
             		editResource(currentNode);
             	} else if (item.id == 'delete') {
             		deleteResource(currentNode);
             	}
             }
         }); 
     });
     
     function initTree() {
         $.ajax({
            url: RESOURCE_URL.GET_ALL,
            success: function(data) {
            	ztree = $.fn.zTree.init($("#resourceList"), setting, data.data);
            	expandNode();
            }
         });
     }
     

     // 展开树节点
     function expandNode() {
    	var nodeId = '${param.expandId}',
    	    node;
   	    if (nodeId) {
   	    	// 获取需要自动展开的节点
   	    	node = ztree.getNodeByParam('id', nodeId, null);
   	    } else {
   	    	// nodeId为空,展开根节点
   	    	node = ztree.getNodeByParam('pId', null, null);
   	    }
   	    if (node){
   	     // 如果该节点不包含子节点,则展开其父节点
   	        if (!node.isParent) {
   	           node = node.getParentNode(); 
   	        }
   	        ztree.expandNode(node, true, false, true);
   	    }
     }
     
     // 查看资源
     function viewResource(event, treeId, treeNode) {
     	var url = RESOURCE_URL.INIT_VIEW+'?id=' + treeNode.id;
     	if (!treeNode.pId) {
     		url = RESOURCE_URL.INIT_STAT;
     	}
     	resourceFrame.src = url;
     }
     // 添加资源
     function addResource(node) {
    	 if (node.type == '4') {
    		 $.sdialog({
    			 type: 'warn',
    			 msg: '无法添加节点！'
    		 });
    		 return false;
    	 }
     	 resourceFrame.src = RESOURCE_URL.INIT_ADD + '?pId=' + node.id + '&level=' + node.level;
     }
     
     // 编辑资源
     function editResource(node) {
         resourceFrame.src = RESOURCE_URL.INIT_EDIT+'?id='+node.id;
     }
     
     //删除资源
     function deleteResource(node) { 
     	$.sdialog({
     		type: 'confirm',
     		msg: '您确定要删除<'+node.name+'>节点吗? 删除时会将该节点下的子节点一并删除',
     		onConfirm: function(result) {
     			if(result){
   			     $.sajax({
     	                url: RESOURCE_URL.DELETE+'?id='+node.id,
     	                onLoadSuccess: function (data) {
     	                	$.stip({
     	                       type:'ok',//显示类型 有info、 ok、 danger
     	                       text: '删除成功！',
     	                       onBeforeHide: function () {
     	                    	  refreshPage(node.pId); 
     	                      }
     	                   });
     	                }
     	            });
     			}
     		}
     	});
     	
     }
     
     // 显示右键菜单
     function showResourceMenu(event, treeId, treeNode) {
     	// 通过treeNode判断是否是在节点上右键
     	if (treeNode) {
	    	 if ( !treeNode.pId ){
	    		$('#resourceMenu').menu("disableItem",$('#resourceMenu').menu("findItem",'删除节点').target);
	    		$('#resourceMenu').menu("disableItem",$('#resourceMenu').menu("findItem",'编辑节点').target);
	    	 } else {
	    		$('#resourceMenu').menu("enableItem",$('#resourceMenu').menu("findItem",'删除节点').target);
	    		$('#resourceMenu').menu("enableItem",$('#resourceMenu').menu("findItem",'编辑节点').target);
	    	 }
     		currentNode = treeNode;
     		ztree.selectNode(currentNode);
            $('#resourceMenu').menu('show',{left:event.pageX, top: event.pageY});	
     	}
     }
     
     // 刷新页面, nodeId为初始化页面时需要自动展开的节点ID
     function refreshPage(nodeId, continueAdd, level) {
    	 nodeId = nodeId || '';
     	 if (continueAdd){
     		var node = ztree.getNodeByParam("id", nodeId, null);
     		ztree.setting.async.url = RESOURCE_URL.GET_TREE + "?pId=" + nodeId;
     		node.isParent = true,
            ztree.updateNode(node);
     		ztree.reAsyncChildNodes(node, "refresh");
     	 } else {
     	    location.href = RESOURCE_URL.INIT_MAIN+'?expandId='+nodeId;
     	 }
     }
     </script>
</body>
</html>
